import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import MainLayout from '../layouts/MainLayout.vue'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    component: MainLayout,
    redirect: '/dashboard',
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../pages/dashboard/index.vue'),
        meta: {
          title: '首页',
          icon: 'dashboard'
        }
      },
      {
        path: 'content',
        name: 'Content',
        redirect: '/content/banners',
        meta: {
          title: '内容管理',
          icon: 'image'
        },
        children: [
          {
            path: 'banners',
            name: 'Banners',
            component: () => import('../pages/content/banners/index.vue'),
            meta: {
              title: '轮播图管理'
            }
          },
          {
            path: 'spots',
            name: 'Spots',
            component: () => import('../pages/content/spots/index.vue'),
            meta: {
              title: '景点管理'
            }
          },
          {
            path: 'articles',
            name: 'Articles',
            component: () => import('../pages/content/articles/index.vue'),
            meta: {
              title: '文章管理'
            }
          }
        ]
      },
      {
        path: 'users',
        name: 'Users',
        component: () => import('../pages/users/index.vue'),
        meta: {
          title: '用户管理',
          icon: 'user',
          roles: ['admin'] // 只有管理员可访问
        }
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('../pages/orders/index.vue'),
        meta: {
          title: '订单管理',
          icon: 'shop'
        }
      },
      {
        path: 'stats',
        name: 'Stats',
        component: () => import('../pages/statistics/index.vue'),
        meta: {
          title: '数据统计',
          icon: 'chart'
        }
      },
      {
        path: 'settings',
        name: 'Settings',
        component: () => import('../pages/settings/index.vue'),
        meta: {
          title: '系统设置',
          icon: 'setting'
        }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('../pages/profile/index.vue'),
        meta: {
          title: '个人中心',
          hidden: true
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../pages/auth/Login.vue'),
    meta: {
      title: '登录',
      public: true
    }
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('../pages/NotFound.vue'),
    meta: {
      title: '页面不存在',
      public: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局路由守卫 - 处理页面标题和权限控制
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = `${to.meta.title || '贵州四季康养'} - 管理后台`
  
  // 检查是否需要登录
  const token = localStorage.getItem('token')
  const isPublicPage = to.meta.public as boolean | undefined
  
  if (!token && !isPublicPage) {
    // 未登录且不是公开页面，重定向到登录页
    next({ name: 'Login', query: { redirect: to.fullPath } })
  } else {
    // 已登录或公开页面，允许访问
    next()
  }
})

export default router 